//
//  MineTimelineTableViewCell.swift
//  MT
//
//  Created by M on 2019/12/25.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit
import MTCategory
import Kingfisher

protocol MineTimelineTableViewCellDelegate: NSObjectProtocol {
    
    /// 点击选择照片媒体
    func timelineCell(_ cell: MineTimelineTableViewCell, didSelectPhotoCollectionWithImageList list: [UserMediaListModel])
    
//    /// 帖子点击删除按钮
//    func timelineCell(_ cell: MineTimelineTableViewCell, didClickRemoveButtonWithModel model: MineInfoListModel)
//
//    /// 展开按钮
//    func timelineCell(_ cell: MineTimelineTableViewCell, didClickExpandButtonWithModel model: MineInfoListModel)
    
    /// 点击暂停播放按钮
    func timelineCell(_ cell: MineTimelineTableViewCell, didClickPlayPauseButton isPlay: Bool)
}

class MineTimelineTableViewCell: UITableViewCell {
    
    /// 代理
    weak var delegate: MineTimelineTableViewCellDelegate?
    
    /// 列表模型左
    var model: MineInfoListModel? {
        didSet {
            guard let model = model else { return }
            containerViewLeft.backgroundColor = .black
            imgCountLabelLeft.text = "\(model.likeCount)"
            likeCountLeft.image = UIImage(named: "like-white")
//            contentView.layoutIfNeeded()
//            let h_1 = MineTimelineTableViewCell.heightOfContainerView(model: model)
//            containerView.snp.updateConstraints { (make) in
//                make.height.equalTo(h_1)
//            }
//
//            /// 创建日期
//            timeLabel.text = model.createTime
//            /// 文字
//            var isExpandButtonHidden: Bool = true
//            if let content = model.content {
//                let containerViewWidth = ScreenWidth - 66 - 45
//                let contentMaxWidth = containerViewWidth - 26
//                contentLabel.text = content
//                var height = content.height(font: PingFangSCMediumFont(16), maxWidth: contentMaxWidth)
//                let rate = contentLabel.font.lineHeight
//                if height > rate * 3.0 && !model.isExpanded {
//                    height = rate * 3.0
//                    isExpandButtonHidden = false
//                }
//                contentLabel.frame = CGRect(x: 13, y: timeLabel.frame.maxY + 8, width: contentMaxWidth, height: height)
//                expandButton.isHidden = isExpandButtonHidden
//                
//            }
            
            if let imgList = model.imgList {
                if imgList.count > 0 {
//                    imgCountLabel.text = "1/\(imgList.count)"
                    if let coverImg = imgList[0].coverImg {
                        coverImageLeft.kf.setImage(with: URL(string: coverImg), placeholder: UIImage(named: defaultPlaceholderIcon60x60))
                    } else if let img = imgList[0].href {
                        coverImageLeft.kf.setImage(with: URL(string: img), placeholder: UIImage(named: defaultPlaceholderIcon60x60))
                    }
                }
//                self.imgCountLabel.isHidden = imgList.first?.objType == 1
//                coverImageLeft.isHidden = false
//                coverImage.snp.remakeConstraints { (make) in
//                    make.top.equalTo(isExpandButtonHidden ? contentLabel.snp.bottom : expandButton.snp.bottom).offset(4)
//                    make.left.equalTo(containerView).offset(13)
//                    make.right.equalTo(containerView).offset(-13)
//                    make.height.equalTo(240)
                }
                
//                if imgList.count > 0 {
//                    imgCountLabel.text = "1/\(imgList.count)"
//                    if let coverImg = imgList[0].coverImg {
//                        coverImage.kf.setImage(with: URL(string: coverImg), placeholder: UIImage(named: "placeholder-square"))
//                    } else if let img = imgList[0].href {
//                        coverImage.kf.setImage(with: URL(string: img), placeholder: UIImage(named: "placeholder-square"))
//                    }
//                }
//                self.imgCountLabel.isHidden = imgList.first?.objType == 1
//
//            } else {
//                coverImage.isHidden = true
//            }
//            if let area = model.area, area.count > 0 {
//                locationIconImageView.isHidden = false
//                areaLabel.isHidden = false
//                areaLabel.text = model.area
//            } else {
//                locationIconImageView.isHidden = true
//                areaLabel.isHidden = true
//            }
        }
    }
    
        /// 列表模型右
        var rightmodel: MineInfoListModel? {
            didSet {
                guard let model = rightmodel else { return }
                containerViewRight.backgroundColor = .black
                imgCountLabelRight.text = "\(model.likeCount)"
                likeCountRight.image = UIImage(named: "like-white")
    //            contentView.layoutIfNeeded()
    //            let h_1 = MineTimelineTableViewCell.heightOfContainerView(model: model)
    //            containerView.snp.updateConstraints { (make) in
    //                make.height.equalTo(h_1)
    //            }
    //
    //            /// 创建日期
    //            timeLabel.text = model.createTime
    //            /// 文字
    //            var isExpandButtonHidden: Bool = true
    //            if let content = model.content {
    //                let containerViewWidth = ScreenWidth - 66 - 45
    //                let contentMaxWidth = containerViewWidth - 26
    //                contentLabel.text = content
    //                var height = content.height(font: PingFangSCMediumFont(16), maxWidth: contentMaxWidth)
    //                let rate = contentLabel.font.lineHeight
    //                if height > rate * 3.0 && !model.isExpanded {
    //                    height = rate * 3.0
    //                    isExpandButtonHidden = false
    //                }
    //                contentLabel.frame = CGRect(x: 13, y: timeLabel.frame.maxY + 8, width: contentMaxWidth, height: height)
    //                expandButton.isHidden = isExpandButtonHidden
    //
    //            }
                
                if let imgList = model.imgList {
                    if imgList.count > 0 {
    //                    imgCountLabel.text = "1/\(imgList.count)"
                        if let coverImg = imgList[0].coverImg {
                            coverImageRight.kf.setImage(with: URL(string: coverImg), placeholder: UIImage(named: defaultPlaceholderIcon60x60))
                        } else if let img = imgList[0].href {
                            coverImageRight.kf.setImage(with: URL(string: img), placeholder: UIImage(named: defaultPlaceholderIcon60x60))
                        }
                    }
    //                self.imgCountLabel.isHidden = imgList.first?.objType == 1
    //                coverImageLeft.isHidden = false
    //                coverImage.snp.remakeConstraints { (make) in
    //                    make.top.equalTo(isExpandButtonHidden ? contentLabel.snp.bottom : expandButton.snp.bottom).offset(4)
    //                    make.left.equalTo(containerView).offset(13)
    //                    make.right.equalTo(containerView).offset(-13)
    //                    make.height.equalTo(240)
                    }
                    
    //                if imgList.count > 0 {
    //                    imgCountLabel.text = "1/\(imgList.count)"
    //                    if let coverImg = imgList[0].coverImg {
    //                        coverImage.kf.setImage(with: URL(string: coverImg), placeholder: UIImage(named: "placeholder-square"))
    //                    } else if let img = imgList[0].href {
    //                        coverImage.kf.setImage(with: URL(string: img), placeholder: UIImage(named: "placeholder-square"))
    //                    }
    //                }
    //                self.imgCountLabel.isHidden = imgList.first?.objType == 1
    //
    //            } else {
    //                coverImage.isHidden = true
    //            }
    //            if let area = model.area, area.count > 0 {
    //                locationIconImageView.isHidden = false
    //                areaLabel.isHidden = false
    //                areaLabel.text = model.area
    //            } else {
    //                locationIconImageView.isHidden = true
    //                areaLabel.isHidden = true
    //            }
            }
        }
    
    /// 线
    lazy var timelineView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        return view
    }()
    
    /// 点
    lazy var dotView: UIImageView = {
        let imgView = UIImageView()
        imgView.image = UIImage(named: "icon_timeline")
        imgView.contentMode = .scaleAspectFill
        return imgView
    }()
    
    /// Container左
    lazy var containerViewLeft: UIView = {
        let view = UIView()
        view.backgroundColor = .black
        return view
    }()
    
    /// Container右
    lazy var containerViewRight: UIView = {
        let view = UIView()
        return view
    }()
    
    /// 时间 Label
    lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.font = PingFangSCRegularFont(12)
        label.textColor = UIColor(hex: 0x5d5d5d)
        label.textAlignment = .left
        return label
    }()
    
    /// content
    lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.font = PingFangSCMediumFont(16)
        label.textColor = UIColor(hex: 0x161616)
        label.textAlignment = .left
        label.numberOfLines = 0
        return label
    }()
    
    /// 删除按钮
    lazy var removeButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("删除", for: .normal)
        btn.setTitleColor(UIColor(hex: 0x5d5d5d), for: .normal)
        btn.titleLabel?.font = PingFangSCRegularFont(12)
        btn.adjustsImageWhenHighlighted = false
//        btn.addTarget(self, action: #selector(timelineTableViewCell(didClickRemoveButton:)), for: .touchUpInside)
        return btn
    }()
    
    /// 展开 / 隐藏按钮
    lazy var expandButton: UIButton = {
        let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 40, height: 17))
        btn.setImage(UIImage(named: "icon_arrow_down_gray"), for: .normal)
        btn.setImage(UIImage(named: "icon_arrow_up_gray"), for: .selected)
        btn.setTitle("展开", for: .normal)
        btn.setTitle("收起", for: .selected)
        btn.setTitleColor(UIColor(hex: 0x5d5d5d), for: .normal)
        btn.titleLabel?.font = PingFangSCRegularFont(12)
        btn.adjustsImageWhenHighlighted = false
        btn.isHidden = true
        if let imageView = btn.imageView { btn.titleEdgeInsets = UIEdgeInsets(top: 0, left: -imageView.frame.width, bottom: 0, right: imageView.frame.width) }
        if let titleLabel = btn.titleLabel { btn.imageEdgeInsets = UIEdgeInsets(top: 0, left: titleLabel.frame.width + 5, bottom: 0, right: -titleLabel.frame.width) }
//        btn.addTarget(self, action: #selector(timelineTableViewCell(didClickExpandButton:)), for: .touchUpInside)
        return btn
    }()
    
    /// 封面图左
    lazy var coverImageLeft: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFit
        imgView.isUserInteractionEnabled = true
//        imgView.tag = 8888
        let t = UITapGestureRecognizer(target: self, action: #selector(timelineTableViewCell(didTapCoverImageView:)))
        imgView.addGestureRecognizer(t)
        return imgView
    }()
    
    /// 点赞icon左
    lazy var likeCountLeft: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFill
        imgView.image = UIImage(named: "like-white")
        return imgView
    }()
    
    /// 封面图右
    lazy var coverImageRight: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFit
        imgView.isUserInteractionEnabled = true
//        imgView.tag = 8889
        let t = UITapGestureRecognizer(target: self, action: #selector(timelineTableViewCell(didTapCoverImageView:)))
        imgView.addGestureRecognizer(t)
        return imgView
    }()
    
    /// 点赞icon右
    lazy var likeCountRight: UIImageView = {
        let imgView = UIImageView()
        return imgView
    }()
    
    /// 视频暂停 / 开始按钮
    lazy var pauseIconButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "icon_start"), for: .normal)
        btn.setImage(UIImage(named: "icon_stop-1"), for: .selected)
        btn.adjustsImageWhenHighlighted = false
        btn.isHidden = true
        btn.addTarget(self, action: #selector(timelineTableViewCell(didClickPlayPauseButton:)), for: .touchUpInside)
        return btn
    }()
    
    /// 点赞数量左
    lazy var imgCountLabelLeft: UILabel = {
        let label = UILabel()
        label.font = PingFangSCMediumFont(12)
        label.textColor = .white
        return label
    }()
    
    /// 点赞数量右
    lazy var imgCountLabelRight: UILabel = {
        let label = UILabel()
        label.font = PingFangSCMediumFont(12)
        label.textColor = .white
        return label
    }()
    
    /// 地标 Icon
    lazy var locationIconImageView: UIImageView = {
        let imgView = UIImageView()
        imgView.image = UIImage(named: "icon_area")
        imgView.contentMode = .scaleAspectFill
        return imgView
    }()
    
    /// 地址 Label
    lazy var areaLabel: UILabel = {
        let label = UILabel()
        label.font = PingFangSCRegularFont(12)
        label.textColor = UIColor(hex: 0x5d5d5d)
        label.textAlignment = .left
        return label
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initializeUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK: ------------------ Overrides ----------------------
extension MineTimelineTableViewCell {
    
    override func prepareForReuse() {
        super.prepareForReuse()
        coverImageLeft.image = nil
        coverImageRight.image = nil
        likeCountRight.image = nil
        likeCountLeft.image = nil
        imgCountLabelRight.text = nil
        imgCountLabelLeft.text = nil
        containerViewRight.backgroundColor = .white
        containerViewLeft.backgroundColor = .white
//        areaLabel.text = nil
//        timeLabel.text = nil
//        imgCountLabel.text = nil
        pauseIconButton.isHidden = true
    }
    
}

// MARK: ------------------ 初始化 ----------------------
extension MineTimelineTableViewCell {
    
    private func initializeUI() {
        selectionStyle = .none
        contentView.backgroundColor = .white
        contentView.addSubview(containerViewLeft)
        contentView.addSubview(containerViewRight)
        
//        contentView.addSubview(timelineView)
//        contentView.addSubview(dotView)
//        contentView.addSubview(containerView)
//        containerView.addSubview(timeLabel)
//        containerView.addSubview(contentLabel)
//        containerView.addSubview(removeButton)
//        containerView.addSubview(expandButton)
        containerViewLeft.addSubview(coverImageLeft)
        coverImageLeft.addSubview(likeCountLeft)
        coverImageLeft.addSubview(imgCountLabelLeft)
        
        containerViewRight.addSubview(coverImageRight)
        coverImageRight.addSubview(likeCountRight)
        coverImageRight.addSubview(imgCountLabelRight)
//        containerView.addSubview(imgCountLabel)
//        containerView.addSubview(locationIconImageView)
//        containerView.addSubview(areaLabel)
        contentView.addSubview(pauseIconButton)
        
//        containerView.snp.makeConstraints { (make) in
//            make.left.equalTo(contentView).offset(66)
//            make.top.equalTo(contentView).offset(24)
//            make.width.equalTo(ScreenWidth - 110)
//            make.height.equalTo(1)
//        }
//
//        timelineView.snp.makeConstraints { (make) in
//            make.centerX.equalTo(contentView.snp.left).offset(37)
//            make.top.bottom.equalTo(contentView)
//            make.width.equalTo(0.5)
//        }
//
//        dotView.snp.makeConstraints { (make) in
//            make.top.equalTo(contentView).offset(33)
//            make.centerX.equalTo(timelineView)
//            make.width.height.equalTo(15)
//        }
//
//        timeLabel.snp.makeConstraints { (make) in
//            make.left.equalTo(containerView).offset(13)
//            make.top.equalTo(containerView).offset(8)
//            make.height.equalTo(17)
//        }

//        removeButton.snp.makeConstraints { (make) in
//            make.centerY.equalTo(timeLabel)
//            make.width.equalTo(24)
//            make.height.equalTo(17)
//            make.right.equalTo(containerView).offset(-13)
//        }
//
//        expandButton.snp.makeConstraints { (make) in
//            make.right.equalTo(contentLabel)
//            make.top.equalTo(contentLabel.snp.bottom).offset(4)
//            make.width.equalTo(40)
//            make.height.equalTo(17)
//        }

        containerViewLeft.snp.makeConstraints { (make) in
            make.left.top.equalToSuperview()
            make.width.equalTo(ScreenWidth/2-2)
            make.bottom.equalTo(contentView).offset(-3)
        }
        
        coverImageLeft.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        likeCountLeft.snp.makeConstraints { (make) in
            make.left.equalTo(10)
            make.bottom.equalTo(-10)
            make.width.height.equalTo(24)
        }
        
        imgCountLabelLeft.snp.makeConstraints { (make) in
            make.centerY.equalTo(likeCountLeft)
            make.left.equalTo(likeCountLeft.snp.right).offset(4)
        }
        
        pauseIconButton.snp.makeConstraints { (make) in
            make.right.bottom.equalTo(coverImageLeft).offset(-12)
            make.width.height.equalTo(55)
        }
        
        containerViewRight.snp.makeConstraints { (make) in
            make.right.top.equalToSuperview()
            make.width.equalTo(ScreenWidth/2-2)
            make.bottom.equalTo(contentView).offset(-3)
        }
        
        coverImageRight.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        likeCountRight.snp.makeConstraints { (make) in
            make.left.equalTo(coverImageRight).offset(10)
            make.bottom.equalTo(-10)
            make.width.height.equalTo(24)
        }
        
        imgCountLabelRight.snp.makeConstraints { (make) in
            make.centerY.equalTo(likeCountRight)
            make.left.equalTo(likeCountRight.snp.right).offset(4)
        }
        
//        imgCountLabel.snp.makeConstraints { (make) in
//            make.bottom.equalTo(coverImage).offset(-5)
//            make.right.equalTo(coverImage).offset(-9)
//        }
//
//        locationIconImageView.snp.makeConstraints { (make) in
//            make.left.equalTo(coverImage)
//            make.top.equalTo(coverImage.snp.bottom).offset(11)
//            make.width.equalTo(12)
//            make.height.equalTo(15)
//        }
//
//        areaLabel.snp.makeConstraints { (make) in
//            make.centerY.equalTo(locationIconImageView)
//            make.left.equalTo(locationIconImageView.snp.right).offset(4)
//        }
        
    }
    
}

// MARK: ------------------ 事件方法 ----------------------
@objc
private extension MineTimelineTableViewCell {
    
//    func timelineTableViewCell(didClickRemoveButton sender: UIButton) {
//        if let delegate = delegate, let model = model {
//            delegate.timelineCell(self, didClickRemoveButtonWithModel: model)
//        }
//    }
//    
//    func timelineTableViewCell(didClickExpandButton sender: UIButton) {
//        if let delegate = delegate, let model = model {
//            delegate.timelineCell(self, didClickExpandButtonWithModel: model)
//        }
//    }
    
    func timelineTableViewCell(didTapCoverImageView gestureRecognizer: UITapGestureRecognizer) {
        if let delegate = delegate, let imgList = model?.imgList {
            delegate.timelineCell(self, didSelectPhotoCollectionWithImageList: imgList)
        }
    }
    
    
    /// 点击播放按钮
    private func timelineTableViewCell(didClickPlayPauseButton sender: UIButton) {
        sender.isSelected = !sender.isSelected
        if let delegate = delegate {
            delegate.timelineCell(self, didClickPlayPauseButton: !sender.isSelected)
        }
    }

    
}

// MARK: ------------------ 高度计算 ----------------------
extension MineTimelineTableViewCell {
    
    class func height(of model: MineInfoListModel) -> CGFloat {
        var height = heightOfContainerView(model: model)
        height += 40
        return height
    }
    
    class func heightOfContainerView(model: MineInfoListModel) -> CGFloat {
        let containerViewWidth = ScreenWidth - 110.0
        let contentMaxWidth = containerViewWidth - 26.0
        
        var height: CGFloat = (8.0 + 17.0 + 8.0)
        var isExpandButtonHidden: Bool = true
        if let content = model.content {
            let font = PingFangSCMediumFont(16)
            var h = (content.height(font: font, maxWidth: contentMaxWidth))
            let rate = font.lineHeight
            if h > rate * 3.0 && !model.isExpanded {
                h = rate * 3.0
                isExpandButtonHidden = false
            }
            height += h
            height += (isExpandButtonHidden ? 8.0 : 13.0)
        }
        if !isExpandButtonHidden {
            height += (17.0 + 4.0)
        }
        if let list = model.imgList, list.count > 0 {
            height += (240.0 + 9.0)
        }
        if let area = model.area, area.count > 0 {
            height += (17.0 + 10.0)
        }
        return height
    }
    
}
